package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service // dubbo的
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    @Override
    public PageBeanVo findVideoByPage(Integer pageNum, Integer pageSize, Long userId) {
        // 1.组装查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId) // 推荐用户的id
        ).with(Sort.by(Sort.Order.desc("date"))) // 时间降序
                .skip((pageNum - 1) * pageSize).limit(pageSize);// 分页

        // 2.查询视频推荐列表
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);

        // 3.查询并封装视频详情
        List<Video> videoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendVideoList)) {
            for (RecommendVideo recommendVideo : recommendVideoList) {
                // 获取视频id
                ObjectId videoId = recommendVideo.getVideoId();
                // 查询视频详情
                Video video = mongoTemplate.findById(videoId, Video.class);
                // 添加到集合中
                videoList.add(video);
            }
        }

        // 4.查询count
        long count = mongoTemplate.count(query, RecommendVideo.class);
        return new PageBeanVo(pageNum, pageSize, count, videoList);
    }

    @Override
    public void saveVideo(Video video) {
        // 1.设置vid
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID));

        // 2.保存
        mongoTemplate.save(video);

        // 3.将视频推荐给自己
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(System.currentTimeMillis());
        recommendVideo.setVid(video.getVid());
        recommendVideo.setUserId(video.getUserId());
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setScore(100D);
        mongoTemplate.save(recommendVideo);
    }

    @Override
    public PageBeanVo findMyVideoByPage(Long uid, Integer pageNum, Integer pageSize) {
        // 1.组装查询条件
        Query query = new Query(Criteria.where("userId").is(uid))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        // 2.查询list
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        // 3.查询couont
        long count = mongoTemplate.count(query, Video.class);
        // 4.封装并返回分页对象
        return new PageBeanVo(pageNum, pageSize, count, videoList);
    }
}
